package com.roy.HadoopRPCDemo;

import java.net.InetSocketAddress;
import java.util.Date;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.io.MapWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.Writable;
import org.apache.hadoop.ipc.RPC;

import com.roy.HadoopRPCDemo.bean.RPCResBean;
import com.roy.HadoopRPCDemo.bean.UserBean;
import com.roy.HadoopRPCDemo.protocol.MyService;
import com.roy.HadoopRPCDemo.util.TextArrayWritable;
import com.roy.HadoopRPCDemo.util.UserBeanArrayWritable;

public class RPCClient {

	public static void main(String[] args) {
		long starttime = (new Date()).getTime();
		InetSocketAddress address = new InetSocketAddress("127.0.0.1",8888);
		MyService proxy;
		Long time;
		
		try {
			//注意，需要客户端与服务端的版本保持一致。
			proxy = RPC.getProxy(MyService.class, 1L, address, new Configuration());
			System.out.println("初始化proxy耗时："+((new Date()).getTime() - starttime));
			
//			System.out.println("基础字段类型服务调用");
//			for(int i = 0 ; i < 10; i ++) {
//				time = (new Date()).getTime();
//				System.out.println(proxy.baseTypeService(100, "baseTypeUser", "123123123"));
//				System.out.println("服务调用耗时："+((new Date()).getTime() - time));
//			}
//			try {
//				Thread.sleep(1000);
//			} catch (InterruptedException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//			System.out.println("Map类型服务调用");
//			for(int i = 0 ; i < 10; i ++) {
//				time = (new Date()).getTime();
//				MapWritable userInfo = new MapWritable();
//				userInfo.put(new Text("userId"), new Text(""+i));
//				userInfo.put(new Text("userName"), new Text("UserName : "+i));
//				userInfo.put(new Text("userPassword"), new Text("UserPassword : "+i));
//				System.out.println(proxy.mapTypeService(userInfo));
//				System.out.println("服务调用耗时："+((new Date()).getTime() - time));
//			}
//			System.out.println("对象类型服务调用");
//			for(int i = 0 ; i < 10; i ++) {
//				time = (new Date()).getTime();
//				UserBean user = new UserBean();
//				user.setUserId(i);
//				user.setUserName("UserName:"+i);
//				user.setUserPassword("UserPassword:"+i);
//				System.out.println(proxy.objTypeService(user));
//				System.out.println("服务调用耗时："+((new Date()).getTime() - time));
//			}
//			System.out.println("对象数组类型服务调用");
//			time = (new Date()).getTime();
//			UserBeanArrayWritable users = new UserBeanArrayWritable();
//			Writable[] userArr = new UserBean[10];
//			for(int i = 0 ; i < 10; i ++) {
//				UserBean user = new UserBean();
//				user.setUserId(i);
//				user.setUserName("UserName:"+i);
//				user.setUserPassword("UserPassword:"+i);
//				userArr[i] = user;
//			}
//			users.set(userArr);
//			UserBeanArrayWritable userBeanArray = proxy.objArrayService(users);
//			System.out.println(userBeanArray);
//			System.out.println("服务调用耗时："+((new Date()).getTime() - time));
//			
			System.out.println("框架类型服务调用");
			
			System.out.println("调用initUser服务");
			time = (new Date()).getTime();
			TextArrayWritable params = new TextArrayWritable();
			Text[] paramArr = new Text[] {new Text("101"),new Text("usreName")};
			params.set(paramArr);
			RPCResBean initUserRes = proxy.frameworkService("initUser", params);
			System.out.println("调用服务initUser完成,res= "+initUserRes);
			System.out.println("服务调用耗时："+((new Date()).getTime() - time));
			
			System.out.println("调用destroyUser服务");
			time = (new Date()).getTime();
			params = new TextArrayWritable();
			paramArr = new Text[] {new Text("102")};
			params.set(paramArr);
			RPCResBean desUserRes = proxy.frameworkService("destroyUser", params);
			System.out.println("调用服务destroyUser完成,res= "+desUserRes);
			System.out.println("服务调用耗时："+((new Date()).getTime() - time));
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
